<?php 
/* SVN FILE: $Id: app_controller.php 68 2007-12-09 22:01:54Z joe.vasquez $ */
/**
 * app controller
 * @filesource
 * @author			Joe Vasquez <joe.vasquez@gmail.com>
 * @copyright		Copyright (c) 2007, Joe Vasquez
 * @package			redlettr
 * @subpackage		subpackages
 * @since			0.1A
 * @version			0.1A  $Rev: 68 $
 * @lastmodified	$Date: 2007-12-09 22:01:54 +0000 (Sun, 09 Dec 2007) $
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */
 
 /**
  * Load Cake Sanitize
  * @link http://api.cakephp.org/1.2/class_sanitize.html
  */
uses('Sanitize');

class AppController extends Controller {
	
/**
 * @var arrray load common helpers
 */
	var $helpers	= array('Html','Form','Crumbs','Time','Session','Javascript');
	
/**
 * @var array crumbs to be displayed in view
 */
	var $crumbs 	= null;
	
/**
 * @var array load common models
 */
	var $uses		= array('User');
	
/**
 * @var array load common components
 */
	var $components	= array('Acl','Menu','Breadcrumbs');
	
/**
 * the Me array holds the currently logged in user's record, refresed on every 
 * page load
 * @var array Me current user model
 */
	var $Me			= false;

/**
 * Checks to see if user is logged in.  If session var uid exists, user data is
 * loaded into {@link Me Me array}, redirects to login page if not logged in
 */	
	function __checkLogin()
	{
		if($this->action != 'login' && $this->action != 'logout')
		{
			if($this->Session->valid() && $this->Session->check('uid'))
			{
				$uid = Security::cipher(
					$this->Session->read('uid'),
					Configure::read('Security.salt')
				);
				$this->User->expects('Profile');
				$this->Me = $this->User->findById(
					$uid,
					array(
						'User.id',
						'User.email',
						'User.created',
						'Profile.id',
						'Profile.displayname',
						'Profile.signature'
					)
				);
				if(empty($this->Me))
				{
					$this->log('Bad UID: '.$this->Session->read('uid'));
					$this->redirect('/logout');
					exit();
				}
				$aro = new Aro();
				$this->Me['aro'] = $aro->findByAlias('User::'.$this->Me['User']['id']);
				$this->Me['aro'] = $this->Me['aro']['Aro'];
				$this->Me['isAdmin'] = ($this->Me['aro']['parent_id'] == 1);
				$this->Me['isMod'] = ($this->Me['isAdmin'] || $this->Me['aro']['parent_id'] == 2);
			} else {
				$this->Session->setFlash('Please login.','error_flash');
				$this->redirect('/login');
				exit();
			}
		}	
	}

/**
 * if current page is an <em>admin</em> location , check to make sure user is an admin
 * exits if user is not an admin
 * @see __isAdminLocation()
 * @see Me
 */	
	function __checkAdmin()
	{
		if($this->__isAdminLocation())
		{
			if(!$this->Me['isAdmin'])
			{
				$this->log($this->Me['User']['id'].' attempted to access the '.$this->action);
				$this->Session->setFlash('Uh oh, you do not have access to that page.','error_flash');
				$this->redirect('/home');
				exit();				
			}
		}
	}
	
/**
 * is current page an <em>admin</em> location?
 * @return bool is current page <em>admin</em> location?
 */
	function __isAdminLocation()
	{
		return (preg_match('/^'.Configure::read('Routing.admin').'_.*$/i',$this->action) == 1);
	}
	
	function __getIntVal($var)
	{
		if(is_numeric($var))
		{
			return intval($var);
		}
		return 0;
	}

/**
 * done before every action
 * 
 * must call Acl startup
 * @see __checkLogin()
 * @see __checkAdmin()
 */	
	function beforeFilter()
	{
		$this->Acl->startup($this);
		$this->__checkLogin();
		$this->__checkAdmin();
	}

/**
 * done before every page render
 * 
 * if user is logged in and is an admin add link to admin section for
 * current component
 * @see Me
 */	
	function beforeRender()
	{
		if($this->Me !== false)
		{
			if(isset($this->Menu))
			{
				$this->Menu->addItem('logout');
				$this->Menu->addItem('home','Actions');
				$this->Menu->addItem('scriptures','Actions');
				
				if($this->Me['isAdmin'] && isset($this->Menu))
				{
					$this->Menu->addItem(
						array(
							$this->name => '/'.Configure::read('Routing.admin').'/'.$this->name
						),
						'Admin Areas'
					);
				}
								
				$this->viewVars['menus'] = $this->Menu->getMenu();
			}
			if(isset($this->Breadcrumbs) && $this->Breadcrumbs->hasCrumbs())
			{
				$this->Breadcrumbs->setCrumbs();
			}
		}
	}  
}
?>